import requests
from lxml import etree
import re
import configparser
import DatabaseSelfCheck as DSC
import threading
lock = threading.Lock()
#配置文件地址
config_path = './config/config.ini'
config = configparser.ConfigParser()
config.read(config_path)

mode = config.get('TEXTMODE','mode')
if mode == 'True':
    mode = 1
else:
    mode = 0
mode = bool(mode)
#↓单独关闭此页调试日志
#mode = False

#线程数
try:
    max_work = int(config.get('NETWORK','max_work'))
except Exception as err:
    max_work = 1
    if mode:
        print(f'0x5-1GetMemory_Thread:{err}')

#获取CPU数据
def get_data_Memory_data_(url,headers,cookies):
    if mode:
        print("Thread started")
    data = requests.get(url,headers=headers,cookies=cookies)
    data = bytes(data.text, 'utf-8').decode('unicode-escape')
    request = etree.HTML(data)

    #品牌
    pattern = r'<title>(.*?)</title>'
    match = re.search(pattern, data)
    pp = match.group(1)
    if 'G.SKILL' in pp :
        pp = 'G.SKILL(芝奇)'
    elif'Mushkin' in pp :
        pp = 'Mushkin'
    elif 'Team' in pp :
        pp = 'Team(十铨)'
    elif 'CORSAIR' in pp :
        pp = 'CORSAIR(海盗船)'
    elif 'OLOy' in pp:
        pp = 'OLOy'
    elif 'Patriot' in pp or 'Patriot':
        pp = 'SPatriot(爱国者)'
    elif 'Kingston' in pp:
        pp = 'Kingston(金士顿)'
    elif 'Silicon' in pp:
        pp = 'Silicon'
    elif 'Crucial' in pp:
        pp = 'Crucial(英睿达)'
    elif 'Neo' in pp:
        pp = 'Neo '
    else:
        pp = pp[0:10]

    if mode:
        print(pp)

    #型号
    pattern = r'<title>(.*?)</title>'
    match = re.search(pattern, data)
    title = match.group(1)

    if mode:
        print('型号'+title[:-13])


    #价格
    jiage = request.xpath('//*[@id="app"]/div[3]/div/div/div/div[1]/div[1]/div[1]/div/ul/li[3]/strong/text()')
    jiage_ = request.xpath('//*[@id="app"]/div[3]/div/div/div/div[1]/div[1]/div[1]/div/ul/li[3]/sup/text()')
    try:
        jg = jiage[0] + jiage_[0]
        if mode:
            print('价格:' + jg + '$')
    except Exception as err:
        jg = 0
        if mode:
            print(f'0x40GetMemory:{err}')

    #成交量
    pattern = r'"LifetimeOrderCount":(\d+)'
    match = re.findall(pattern, data)
    cjl = 0
    for i in match:
        cjl += int(i)
    if mode:
        print('成交量:',cjl)
    with lock:
        try:
            DSC.save_data('memory',pp,title[:-13],jg,cjl)
        except Exception as err:
            if mode:
                print(f'0x043mysql_Memory{err}')

from tqdm import tqdm
import concurrent.futures
def get_data_Memory_data(data,headers,cookies):
    urls = data.xpath('//*[@class="item-cell"]/div/a/@href')
    if mode:
        print( urls)
        print(len( urls))
    if not urls:
        return
    with concurrent.futures.ThreadPoolExecutor(max_workers=max_work) as executor:
        futures = []
        for url in urls:
            future = executor.submit(get_data_Memory_data_, url, headers, cookies)
            futures.append(future)
        if not mode:
            progress_bar = tqdm(total=len(futures), desc='Progress', unit='task')
        for future in concurrent.futures.as_completed(futures):
            # 处理每个线程的结果
            result = future.result()
            if not mode:
                progress_bar.update(1)
    #get_data_Memory_data_( urls,headers,cookies)
